package com.fs.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.SysAdminAndRoleMapper;
import com.fs.mapper.SysAdminRoleMenuMapper;
import com.fs.mapper.SysAdminRolesMapper;
import com.fs.pojo.SysAdminAndRoles;
import com.fs.pojo.SysAdminRoleMenus;
import com.fs.pojo.SysAdminRoles;
import com.fs.pojo.User;
import com.fs.service.SysAdminRoleService;
import com.fs.vo.PageResult;
import com.fs.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service("sysAdminRoleService")
public class SysAdminRoleServiceImpl implements SysAdminRoleService {
    @Autowired
    private SysAdminRolesMapper sysAdminRolesMapper;

    @Autowired
    private SysAdminRoleMenuMapper sysAdminRoleMenuMapper;

    @Autowired
    private SysAdminAndRoleMapper sysAdminAndRoleMapper;

    @Override
    public SysResult getList(PageResult request) {
//        List<SysAdminRolesEntity> rolesEntityList = sysAdminRolesDao.selectList(null);


        Integer pageNum = request.getPageNum();
        Integer pageSize = request.getPageSize();
        if (pageNum < 0) pageNum = 1;
        if (pageSize < 0) pageSize = 10;
        Page<SysAdminRoles> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysAdminRoles> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StrUtil.isBlank(request.getQuery()), "role_name", request.getQuery())
                .orderByDesc("id");
        page = sysAdminRolesMapper.selectPage(page, queryWrapper);
        request.setTotal(page.getTotal())
                .setRows(page.getRecords());
        return SysResult.success(request);
    }

    @Transactional
    @Override
    public SysResult saveRole(SysAdminRoles role) {

//        role.setCreateTime(new Date());
        sysAdminRolesMapper.insert(role);

        //检查权限是否越权
//        checkPrems(role);

        //保存角色与菜单关系
//        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
        List<Integer> roleMenuIdList = role.getMenuIdList();
        for (Integer rid : roleMenuIdList) {
            if(rid.equals(-666666))continue;
            SysAdminRoleMenus entity = new SysAdminRoleMenus();
            entity.setRoleId(role.getId()).setMenuId(rid);
            sysAdminRoleMenuMapper.insert(entity);
        }
        return SysResult.success();
    }

    @Transactional
    @Override
    public SysResult deleteBatch(Long[] roleIds) {
        //删除角色
        QueryWrapper<SysAdminRoles> wrapper1 = new QueryWrapper<>();
        wrapper1.in("id",roleIds);
        sysAdminRolesMapper.delete(wrapper1);

        //删除角色与菜单关联
        QueryWrapper<SysAdminRoleMenus> wrapper2 = new QueryWrapper<>();
        wrapper2.in("role_id",roleIds);
        sysAdminRoleMenuMapper.delete(wrapper2);//.deleteBatch(roleIds);

        //删除角色与用户关联
        QueryWrapper<SysAdminAndRoles> wrapper3 = new QueryWrapper<>();
        wrapper3.in("role_id",roleIds);
        sysAdminAndRoleMapper.delete(wrapper3);

        return SysResult.success();
    }

    @Override
    public SysAdminRoles getById(Long roleId) {
        return sysAdminRolesMapper.selectById(roleId);
    }

    @Transactional
    @Override
    public SysResult update(SysAdminRoles role) {
        sysAdminRolesMapper.updateById(role);

        //检查权限是否越权
//        checkPrems(role);

        //更新角色与菜单关系
        List<Integer> roleMenuIdList = role.getMenuIdList();
//        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
        QueryWrapper<SysAdminRoleMenus> wrapper2 = new QueryWrapper<>();
        wrapper2
//                .in("menu_id",roleMenuIdList)
                .eq("role_id", role.getId());
        sysAdminRoleMenuMapper.delete(wrapper2);

        for (Integer rid : roleMenuIdList) {
            if(rid.equals(-666666))continue;
            SysAdminRoleMenus entity = new SysAdminRoleMenus();
            entity.setRoleId(role.getId()).setMenuId(rid);
            sysAdminRoleMenuMapper.insert(entity);
        }
        return SysResult.success();
    }

    /**
     * 检查权限是否越权
     */
//    private void checkPrems(SysAdminRolesEntity role){
//        //如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
//        if(role.getCreateUserId() == Constant.SUPER_ADMIN){
//            return ;
//        }
//
//        //查询用户所拥有的菜单列表
//        List<Long> menuIdList = sysUserService.queryAllMenuId(role.getCreateUserId());
//
//        //判断是否越权
//        if(!menuIdList.containsAll(role.getMenuIdList())){
//            throw new RRException("新增角色的权限，已超出你的权限范围");
//        }
//    }
}
